
function l(){
    var xmlhttp;
    xmlhttp=new XMLHttpRequest();

    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            var json = JSON.parse(xmlhttp.responseText);
            console.log(json.data);

            json = json.data.reverse();

            for(var k = 0;k<json.length;k++){

                var li = document.createElement("li");

                var span = document.createElement("span");
                span.innerHTML = json[k].studentName;

                var span1 = document.createElement("span");
                span1.innerHTML = json[k].sex;

                var span2 = document.createElement("span");
                span2.innerHTML = json[k].age;

                li.appendChild(span);
                li.appendChild(span1);
                li.appendChild(span2);

                document.querySelector("#list").appendChild(li);
                //document.querySelector("#n").value = "";
                //document.querySelector("#a").value = "";

            }
        }
    };
    xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/student",true);
    xmlhttp.send();
}
l();




document.querySelector("#btn").addEventListener("click",function(){
    var n = document.querySelector("#n").value;
    console.log(n);
    var s = document.querySelector("#s").value;
    var a = document.querySelector("#a").value;

    if(a==""&&n==""){
        alert("请输入正确的添加内容")
    }else {
        var xmlhttp;
        xmlhttp=new XMLHttpRequest();

        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.querySelector("#list").innerHTML = "";
                l();
            }
        };
        xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/addStudent?studentName="+n+"&sex="+s+"&age="+a,true);
        xmlhttp.send();
    }
});

